home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / thsfs.tgz / thsfs.tar / thsfs / kompr.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  4KB  |  218 lines

  1. /*************************************************************
  2.  *                                                           *
  3.  *    ths  Filesystem                  04.10.94      V1.1    *
  4.  *                                                           *
  5.  *    Thomas Scheuermann     ths@ai-lab.fh-furtwangen.de     *
  6.  *                                                           *
  7.  *************************************************************/
  8.  
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/stat.h>
  15. #include <linux/mm.h>
  16. #include <linux/locks.h>
  17. #include <linux/fs.h>
  18. #include <linux/malloc.h>
  19.  
  20. #include <asm/system.h>
  21. #include <asm/segment.h>
  22. #include <asm/bitops.h>
  23.  
  24. #include "ths.h"
  25. #include "ths_i.h"
  26.  
  27. /*
  28.  * Test auf 44530002
  29.  */
  30.  
  31. int arttest(long *data)
  32. {
  33.     if(*data == 0x02005344)
  34.         return 0;
  35.     else
  36.         return 1;
  37. }
  38.  
  39. /*
  40.  * Daten holen
  41.  */
  42.  
  43. void nextdata(unsigned char **data, int *bz, int *cc,long *zc,struct super_block *s,long sektor, struct buffer_head **bh)
  44. {
  45.     while(*bz < 25)
  46.     {
  47.         if (!(*cc % 512))
  48.             *data = ths_uread(s,sektor+(*cc)/512,bh);
  49.         *zc |= (unsigned long)(*data)[*cc%512]<<*bz;
  50.         (*cc)++;
  51.         if (!(*cc % 512))
  52.             brelse(*bh);
  53.         *bz +=8;
  54.     }
  55. }
  56.  
  57. /*
  58.  * Dekompressionsalgorithmus
  59.  * funktioniert nur mit Komprimierung 2
  60.  */
  61.  
  62. void decompress(struct super_block *s, struct ths_buffer *tbf, int mdfat)
  63. {
  64.     unsigned char zu,flag,*data=NULL;
  65.     unsigned long sektor,zc;
  66.     struct buffer_head *bh;
  67.     int k,rb,rl,hz,m,sek,sekmax,bz,cc,test;
  68.  
  69.  
  70. #ifdef DEBUG
  71.     printk("decompress %d\n",mdfat);
  72. #endif
  73.  
  74.     sektor = (mdfat & 0x1fffff) +1;
  75.     sekmax = ((mdfat & 0x3c000000)>>26) +1;
  76.  
  77. /*
  78.  * i    : Bitzaehler
  79.  * k    : Zeichenzaehler dekomprimiert
  80.  * hz    : Hilfszaehler fuer Bits;
  81.  * rb    : Rueckwaertsbezug rb Bytes
  82.  * rl    : Laenge des Rueckwaertsbezugs
  83.  * m    : Laengenhilfsspeicher
  84.  * flag    : Status
  85.  * zc    : komprimierte Daten(max 4 Byte)
  86.  * bz    : Bitzaehler fuer zc
  87.  * cc    : Zeichenzaehler fuer unkomprimierte Zeichen
  88.  * zu    : unkomprimiertes Zeichen
  89.  * sek    : Sektorzaehler
  90.  */
  91.     k=0;
  92.     zu=0;
  93.     zc=0;
  94.     rb=0;
  95.     rl=0;
  96.     hz=0;
  97.     m=0;
  98.     sek = 0;
  99.     flag = 'n';
  100.     bz=0;
  101.     cc=0;
  102.     test=0;
  103.  
  104.     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  105.     if(arttest(&zc))
  106.     {
  107.         printk("Komprimierung nicht erkannt !\n");
  108.         return;
  109.     }
  110.     zc=0;
  111.     bz=0;
  112.     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  113.  
  114.  
  115.     for(;sek<sekmax;)
  116.     {
  117.         switch(zc&0x03)
  118.         {
  119.             case 2:        /* 0 - 127 */
  120.                 zc>>=2;
  121.                 zu = (unsigned char)zc & 0x7f;
  122.                 zc>>=7;
  123.                 bz -=9;
  124.                 (tbf->data[k/512])[k%512] = zu;
  125.                 k++;
  126.                 if(bz<17)
  127.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  128.                 break;
  129.             case 1:        /* 128 - 255 */
  130.                 zc>>=2;
  131.                 zu = (unsigned char)(zc & 0x7f)|128;
  132.                 zc>>=7;
  133.                 bz -=9;
  134.                 (tbf->data[k/512])[k%512] = zu;
  135.                 k++;
  136.                 if(bz<17)
  137.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  138.                 break;
  139.             case 0:        /* 6 Bit Rueckbezug */
  140.                 zc>>=2;
  141.                 rb = zc & 0x3f;
  142.                 zc>>=6;
  143.                 bz -=8;
  144.                 if(bz<17)
  145.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  146.                 hz=0;
  147.                 for(;!(zc&0x01);zc>>=1,hz++);
  148.                 zc>>=1;
  149.                 bz-=(hz+1);
  150.                 rl = (zc & ((1<<hz)-1)) + (1<<hz)+1;
  151.                 zc>>=hz;
  152.                 bz -=hz;
  153.                 if(bz<17)
  154.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  155.                 for(hz=0;hz<rl;hz++)
  156.                 {
  157.                     (tbf->data[k/512])[k%512] = (tbf->data[(k-rb)/512])[(k-rb)%512];
  158.                     k++;
  159.                 }
  160.                 break;
  161.             case 3:        /* Rueckbezug auf 65+ oder 320+ Zeichen */
  162.                 switch(zc&0x07)
  163.                 {
  164.                     case 3: /* 65+ Zeichen */
  165.                         zc>>=3;
  166.                         rb = (zc & 0xff) + 64;
  167.                         zc>>=8;
  168.                         bz -=11;
  169.                         break;
  170.                     case 7: /* 320+ Zeichen */
  171.                         zc>>=3;
  172.                         rb = (zc & 0xfff);
  173.                         if(rb == 0xfff)
  174.                             test=1;
  175.                         rb+=320;
  176.                         zc>>=12;
  177.                         bz -=15;
  178.                         break;
  179.                 }
  180.                 if(bz<17)
  181.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  182.                 if(test)
  183.                 {
  184.                     test=0;
  185.                     sek++;
  186.                     break;    /* Sonderfall 512 Byte Grenze */
  187.                 }
  188.                 hz=0;
  189.                 for(;!(zc&0x01);zc>>=1,hz++);
  190.                 zc>>=1;
  191.                 bz-=(hz+1);
  192.                 rl = (zc & ((1<<hz)-1)) + (1<<hz)+1;
  193.                 zc>>=hz;
  194.                 bz -=hz;
  195.                 if(bz<17)
  196.                     nextdata(&data,&bz,&cc,&zc,s,sektor,&bh);
  197.                 for(hz=0;hz<rl;hz++)
  198.                 {
  199.                     (tbf->data[k/512])[k%512] = (tbf->data[(k-rb)/512])[(k-rb)%512];
  200.                     k++;
  201.                 }
  202.                 break;
  203.         }
  204.     }
  205.     if((cc%512))
  206.     {
  207.         brelse(bh);
  208.     }
  209. #ifdef DEBUG
  210.     printk("decompress Ende\n");
  211. #endif
  212.     return;
  213. }
  214.  
  215.  
  216.  
  217.  
  218.